// ************************* 78 character ruler *****************************
// RFBB.c -- Implementation of FRIG RFBB 
//
// $Author: Liu Rongwu $
// $Header: /RFBB_TDDSW/FZHA_RFBB/Sources/RFBB/StartPingTest.c
// $History: 09/25/2010 create and modify
//
// (c) 2008 Nokia Siemens Networks.
// ************************* 78 character ruler *****************************
#include "Ping.h" 
#include "RFBB.h"
#include <utility.h>
#include <cvintwrk.h>

// -----------------------------------------------------------------------
// Function: StartupPingTest  
// Purpose:  Ping DUT to check it has started up
//           The purpose of the test is check that connection (Ethernet) between the tester and DUT is working.
// Type:     Pass/Fail
//
#if(ON_OFF != SIMU_VERS)

void __decldllexp TX_TEST StartupPingTest(tTestData *testData, tTestError *testError)
{
   int error = 0;
   ErrMsg errMsg = {'\0'};
   ERRORINFO errorInfo;
   
//   HRESULT hResult;
   CAObjHandle hStepName = 0;
   CAObjHandle hExecution = 0;
   
   TSObj_Property hMonitorData = 0;
   VBOOL varUserTerminated;
   VBOOL varExists;
   
   char* pszStepName = 0; 
   char szPropertyName[255] = {'\0'}; 
   char* pszTargetName = 0; 
   char szLogMsg[255] = {'\0'};
   char szSystemStatus[50];

   int iPingStatus = 0;
   int iTimeToWaitReply = 0;
   int iContinue = 1;
   int iCounter = 0;

   const int iDefaultTimeOut = 500;
   const int iDefaultRepeats = 1;
   const int iDefaultPacketSize = 256;


   double dTimeElapsed = 0.0;
   double dStartTime = 0.0;
   
   //---------------------------------------------------------------------
	// Testcase information
	//---------------------------------------------------------------------	
   // Resolve StepName 
   tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
		                               TS_SeqContextStep, CAVT_OBJHANDLE, &hStepName));
   
   tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, 
	                            TS_StepName, CAVT_CSTRING, &pszStepName));
	
   // write Stepname to eventlog
   sprintf( szLogMsg, "##### %s #####", pszStepName );
   ErrChk( EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg) );
   
   // Post message to GUI
   
   PostMessageToGui(pszStepName);
   
 	//--------------------------------------------------------------------
	// Get Test Parameters from Sequence FileGlobals
   //--------------------------------------------------------------------
   sprintf(szPropertyName, "%s.%s.PARAMETERS.IPADDRESS", TESTPLAN_BASE_PATH, pszStepName); 
   // Check if IP address available in test parameter
   tsErrChk(TS_PropertyExists (testData->seqContextCVI, NULL, szPropertyName, 0, &varExists));
   // If it does not exist, use address from common parameters
   if( varExists == VARIANT_FALSE ) {
      sprintf(szPropertyName, "%s.COMMON.IPADDRESS_DUT", TESTPLAN_BASE_PATH);   
   } 
   tsErrChk(TS_PropertyGetValString (testData->seqContextCVI, &errorInfo, szPropertyName, 0, &pszTargetName));
   
   
   sprintf(szPropertyName, "%s.%s.PARAMETERS.TIMETOWAITREPLY", TESTPLAN_BASE_PATH, pszStepName);
   tsErrChk(TS_PropertyGetValInteger (testData->seqContextCVI, &errorInfo, szPropertyName, 0, &iTimeToWaitReply));
   
   sprintf( szLogMsg, "Startup ping for target \"%s\"", pszTargetName );
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));

   //--------------------------------------------------------------------
	// The actual test.....
   //--------------------------------------------------------------------
   dStartTime = Timer(); 
   // Initialise termination monitor for polling button <STOP TESTING>
   tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo,
                                      TS_SeqContextExecution, CAVT_OBJHANDLE,
                                      &hExecution));
   tsErrChk(TS_ExecutionInitTerminationMonitor (hExecution, &errorInfo, &hMonitorData));
   
   do {
      
          // Post message to GUI
          sprintf( szSystemStatus, "Ping... packet nbr: %3d", iCounter++ );
    	  
          // Post message to GUI
          PostMessageToGui(szSystemStatus);
    	  
          iPingStatus = Ping( pszTargetName, iDefaultPacketSize, iDefaultTimeOut, iDefaultRepeats );
            
          if( iPingStatus < 0 ) { // negative return value indicates fatal error
             sprintf( errMsg, "Error in Ping, target = \"%s\"", pszTargetName );
             error = -1;
             goto Error;
          }
          if( iPingStatus == 0 ) { // target replied to ping
             sprintf( szLogMsg, "Got response from \"%s\"", pszTargetName);
             ErrChk(EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ));
             break; // dirty way to stop the while loop but the target replied 
                    // so we got no reason to hang around here...
          }
          
          // Check if user wants to terminate <STOP TESTING>
          tsErrChk(TS_ExecutionGetTerminationMonitorStatus (hExecution, &errorInfo,
                                                            hMonitorData,
                                                            CA_DEFAULT_VAL,
                                                            &varUserTerminated));
          if( varUserTerminated == VARIANT_TRUE ) { 
             sprintf( errMsg, "\"%s\" - Terminated by user!", pszStepName );
             ErrChk(EELog_INEventLogLogEvent( g_hEventLog, NULL, errMsg ));
             error = 1; // Warning -> Step status will be "Terminated"
             goto Error;
          }

          Sleep(1000);
          
          dTimeElapsed = Timer() - dStartTime;
          if( dTimeElapsed > iTimeToWaitReply ) 
          {
             iContinue = 0;

             ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, "Request timed out."));
          }
   } while( iContinue != 0 );
   
   
   // Evaluate test status
   if( iPingStatus != 0 || iContinue == 0) {
      testData->result = FAIL;
   }
   else {
      testData->result = PASS;
   }

Error:  
   // FREE RESOURCES
   CA_DISCARD_OBJ( hStepName );
   CA_DISCARD_OBJ( hMonitorData );
   CA_DISCARD_OBJ( hExecution );

   CA_FREE( pszStepName );
   CA_FREE( pszTargetName );
	
   // If an error occurred, set the error flag to cause a run-time error in TestStand.
   if (error < 0)
   {
      EELog_INEventLogShow(g_hEventLog,NULL);            	
      testError->errorFlag = TRUE; 
      testError->errorCode = error;
      testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
   }
     
   tsErrChk(TS_ThreadPostUIMessage (g_hThreadPostUIMessage, NULL,
                                    TS_UIMsg_ProgressText, 0.0,
                                    " ", VTRUE));
   return;    
}

#else
void __decldllexp TX_TEST StartupPingTest(tTestData *testData, tTestError *testError)
{
   int error = 0;
   ErrMsg errMsg = {'\0'};
   ERRORINFO errorInfo;
   
//   HRESULT hResult;
   CAObjHandle hStepName = 0;
   CAObjHandle hExecution = 0;
   CAObjHandle hThreadPostUIMessage = 0; 
   
   TSObj_Property hMonitorData = 0;
   VBOOL varUserTerminated;
   VBOOL varExists;
   
   char* pszStepName = 0; 
   char szPropertyName[255] = {'\0'}; 
   char* pszTargetName = 0; 
   char szLogMsg[255] = {'\0'};
   char szSystemStatus[50];

   int iPingStatus = 0;
   int iTimeToWaitReply = 0;
   int iContinue = 1;
   int iCounter = 0;

   const int iDefaultTimeOut = 500;
   const int iDefaultRepeats = 1;
   const int iDefaultPacketSize = 256;


   double dTimeElapsed = 0.0;
   double dStartTime = 0.0;
   
   //---------------------------------------------------------------------
	// Testcase information
	//---------------------------------------------------------------------	
   // Resolve StepName 
   tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
		                               TS_SeqContextStep, CAVT_OBJHANDLE, &hStepName));
   
   tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, 
	                            TS_StepName, CAVT_CSTRING, &pszStepName));
	
   // write Stepname to eventlog
   sprintf( szLogMsg, "##### %s #####", pszStepName );
   ErrChk( EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg) );
   
   // Post message to GUI
   
   PostMessageToGui(pszStepName);
   
 	//--------------------------------------------------------------------
	// Get Test Parameters from Sequence FileGlobals
   //--------------------------------------------------------------------
   sprintf(szPropertyName, "%s.%s.PARAMETERS.IPADDRESS", TESTPLAN_BASE_PATH, pszStepName); 
   // Check if IP address available in test parameter
   tsErrChk(TS_PropertyExists (testData->seqContextCVI, NULL, szPropertyName, 0, &varExists));
   // If it does not exist, use address from common parameters
   if( varExists == VARIANT_FALSE ) {
      sprintf(szPropertyName, "%s.COMMON.IPADDRESS_DUT", TESTPLAN_BASE_PATH);   
   } 
   tsErrChk(TS_PropertyGetValString (testData->seqContextCVI, &errorInfo, szPropertyName, 0, &pszTargetName));
   
   
   sprintf(szPropertyName, "%s.%s.PARAMETERS.TIMETOWAITREPLY", TESTPLAN_BASE_PATH, pszStepName);
   tsErrChk(TS_PropertyGetValInteger (testData->seqContextCVI, &errorInfo, szPropertyName, 0, &iTimeToWaitReply));
   
   sprintf( szLogMsg, "Startup ping for target \"%s\"", pszTargetName );
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));

   //--------------------------------------------------------------------
	// The actual test.....
   //--------------------------------------------------------------------
   


Error: 

   // Evaluate test status
   if( iPingStatus != 0 || iContinue == 0) {
      testData->result = FAIL;
   }
   else {
      testData->result = PASS;
   }

    
   // FREE RESOURCES
   CA_DISCARD_OBJ( hStepName );
   CA_DISCARD_OBJ( hMonitorData );
   CA_DISCARD_OBJ( hExecution );
   CA_DISCARD_OBJ( hThreadPostUIMessage);

   CA_FREE( pszStepName );
   CA_FREE( pszTargetName );
	
   // If an error occurred, set the error flag to cause a run-time error in TestStand.
   if (error < 0)
   {
      EELog_INEventLogShow(g_hEventLog,NULL);            	
      testError->errorFlag = TRUE; 
      testError->errorCode = error;
      testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
   }
     
   tsErrChk(TS_ThreadPostUIMessage (g_hThreadPostUIMessage, NULL,
                                    TS_UIMsg_ProgressText, 0.0,
                                    " ", VTRUE));
   return;    
}
#endif
/****************************************************************************
 *---------------------------- End source File -----------------------------*
 ****************************************************************************/

